package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * 在Test2的基础上，要求调用@AutoRunMethod注解标注的方法若干次
 * 次数由@AutoRunMethod注解的value参数决定
 */
public class Test3 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入一个类：");
        String className= scan.nextLine();


        Class cls=Class.forName(className);
        if(cls.isAnnotationPresent(AutoRunClass.class)){
            Object o = cls.newInstance();
            Method[] methods = cls.getDeclaredMethods();
            for (Method method:methods){
                if(method.isAnnotationPresent(AutoRunMethod.class)){
                    AutoRunMethod autoRunMethod=method.getAnnotation(AutoRunMethod.class);
                    int value= autoRunMethod.value();
                     System.out.println(method.getName()+"方法被@AutoRunMethod注解标注，调用"+value+"次");
                    for (int i = 0; i < value; i++) {
                        method.invoke(o);
                    }

                }
            }
        }else{
            System.out.println("该类没有被@AutoRunMethod注解标注");
        }


    }


}
